import * as PIXI from "pixi.js";
let realPath;
let shapeContainer;
let beginPos;
let pos = [];

function drawPos(main) {
  if(!realPath) return;
  realPath.clear();
  // let w = Math.abs(beginPos.x - main.x);
  // let h = Math.abs(beginPos.y - main.y);
  let arr = [];
  let xPos = [beginPos.x,main.x];
  let yPos = [beginPos.y,main.y]
  for (let i = Math.min(...xPos); i <= Math.max(...xPos); i++) {
    for (let j = Math.min(...yPos); j <= Math.max(...yPos); j++) {
      if (i == beginPos.x || j == beginPos.y || i == main.x || j == main.y) {
        arr.push({ x: i, y: j });
      }
    }
  }
  pos = arr;
  pos.forEach((p) => {
    realPath.beginFill(main.pointColor, 1);
    realPath.drawRect(p.x, p.y, 1, 1);
    realPath.endFill();
  });
}

export default function (pxs, main) {
  // if(main.isUp) return;
  if (main.eventName == "pointerDown") {
    shapeContainer = main.shapeContainer;
    realPath = new PIXI.Graphics();
    shapeContainer.addChild(realPath);
    beginPos = pxs[0];
  }
  if (main.eventName == "pointerMove" && !main.isUp) {
    drawPos(main);
  }
  if (main.eventName == "pointerUp" || main.eventName == "pointerOut") {
    if (!realPath) return;
    realPath.destroy(true, true);
    realPath = null;
    if (pos.length == 0) return;
    main.pxMap.forEach((px) => {
      if (pos.some((p) => p.x == px.x && p.y == px.y)) {
        px.color = main.pointColor;
        px.alpha = 1;
      }
    });
    pos.length = 0;
    main.update();
  }
}
